package controllers;

import dao.IMovieDao;
import dao.IMovieTheatreDao;
import models.User;
import play.Logger;
import play.mvc.Before;
import play.mvc.Controller;
import serviceImpl.MovieDaoImpl;
import serviceImpl.MovieTheatreDaoImpl;
import utils.common.TimeUtil;

import java.util.Date;

/**
 * Created by 国彬 on 2016/7/3.
 */
public class MovieAction extends Controller {
    static MovieDaoImpl movieDao = new MovieDaoImpl();
    static IMovieTheatreDao movieTheatreDao = new MovieTheatreDaoImpl();
    @Before
    static void checkLogin(){
        renderArgs.put("cxt",request.host);
        if(session.get("user")!=null){
            renderArgs.put("user", User.findById(session.get("user")));
        }
    }
    /**
     * 获得正在上映电影
     */
    public static void fetchOpeningMovie(){
        int fetch = request.params.get("fetch",Integer.class);
        WebService.wsOk(movieDao.fetchOpeningMoive(fetch));
    }

    /**
     * 获得即将上映的电影
     */
    public static void fetchWaitingMovie(){
        int fetch = request.params.get("fetch",Integer.class);
        WebService.wsOk(movieDao.fetchWaitingMovie(fetch));
    }

    /**
     * 获得某部电影的详细信息
     * @param id
     */
    public static void movieDetail(String id){
        Logger.debug("id=",id);
        WebService.wsOk(movieDao.findDetailById(id));
    }

    /**
     * 获得某部电影的详细信息
     */
    public static void movieDetail(){
        String id = request.params.get("id");
        Logger.debug("id=",id);
        WebService.wsOk(movieDao.findDetailById(id));
    }

    /**
     * 根据日期查找上映的电影
     */
    public static void findMovieByDate(){
        String date = request.params.get("date");
        WebService.wsOk(movieDao.findMovieByDate(TimeUtil.stringToDate(date,"yyyy-MM-dd")));
    }

    /**
     * 根据电影id,时间，查询上映影院
     * @param movieId
     */
    public static void findPublishTheatre(String movieId,String date){
        Date time = null;
        if(null == date||date.equals("")){
            time = new Date();
        }else{
            time = TimeUtil.stringToDate(date,"yyyy-MM-dd");
        }
        WebService.wsOk(movieTheatreDao.findByMovieIdDate(movieId,time));
    }


    /**
     * 根据电影id，影院id，时间（yyyy-MM-dd HH:mm:ss）查询该影院上映场次
     */
    public static void findByMovieTheatreIdAndDate(){
        String movieId = request.params.get("movieId");
        String theatreId = request.params.get("theatreId");
        String date = request.params.get("date");
        WebService.wsOk(movieDao.findByMovieTheatreIdAndDate(movieId,theatreId,TimeUtil.stringToDate(date,"yyyy-MM-dd HH:mm:ss")));
    }


}
